﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VPN.Manage;
using System.Collections.Generic;

namespace VPNNew.UserContorls
{
    public partial class POSearch : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                POSearchArg arg = new POSearchArg(Request.QueryString["q"]);
                txtUserName.Text = arg.UserName;
                txtPONumber.Text = arg.PONumber;
                txtCDS.Text = arg.CreateDateStart.HasValue ? arg.CreateDateStart.Value.ToString("yyyy-MM-dd") : string.Empty;
                txtCDE.Text = arg.CreateDateEnd.HasValue ? arg.CreateDateEnd.Value.ToString("yyyy-MM-dd") : string.Empty;
                txtPDS.Text = arg.PayDateStart.HasValue ? arg.PayDateStart.Value.ToString("yyyy-MM-dd") : string.Empty;
                txtPDE.Text = arg.PayDateEnd.HasValue ? arg.PayDateEnd.Value.ToString("yyyy-MM-dd") : string.Empty;
                ddlStatus.SelectedValue = ((int)arg.POStatus).ToString();

                var ps = from int k in Enum.GetValues(typeof(POStatus))
                         select new { Key = k, Value = Enum.GetName(typeof(POStatus), k) };

                ddlStatus.DataSource = ps;
                ddlStatus.DataValueField = "Key";
                ddlStatus.DataTextField = "Value";
                ddlStatus.DataTextFormatString = "[[{0}]]";
                ddlStatus.DataBind();


            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("{0}?q={1}", Request.Path
                , new POSearchArg()
                {
                    UserName = txtUserName.Text.Trim(),
                    PONumber = txtPONumber.Text.Trim(),
                    POStatus = (POStatus)Enum.Parse(typeof(POStatus), ddlStatus.SelectedValue),
                    CreateDateStart = string.IsNullOrEmpty(txtCDS.Text.Trim()) ? (DateTime?)null : DateTime.Parse(txtCDS.Text.Trim()),
                    CreateDateEnd = string.IsNullOrEmpty(txtCDE.Text.Trim()) ? (DateTime?)null : DateTime.Parse(txtCDE.Text.Trim()),
                    PayDateStart = string.IsNullOrEmpty(txtPDS.Text.Trim()) ? (DateTime?)null : DateTime.Parse(txtPDS.Text.Trim()),
                    PayDateEnd = string.IsNullOrEmpty(txtPDE.Text.Trim()) ? (DateTime?)null : DateTime.Parse(txtPDE.Text.Trim()),
                }.ToString())
                );
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.Path);
        }



    }
}